package yaner.hotel.manager.resource.service.impl;

import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.dto.room.RoomAddDto;
import yaner.hotel.manager.common.dto.room.RoomDto;
import yaner.hotel.manager.common.dto.room.RoomUpdateDto;
import yaner.hotel.manager.common.vo.RoomVo;
import yaner.hotel.manager.resource.dao.IRoomDao;
import yaner.hotel.manager.resource.entity.Room;
import yaner.hotel.manager.resource.mapper.IRoomMapper;
import yaner.hotel.manager.resource.service.IRoomService;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl implements IRoomService {
    private final IRoomDao roomDao;
    private final IRoomMapper roomMapper;

    public RoomServiceImpl(IRoomDao roomDao, IRoomMapper roomMapper) {
        this.roomDao = roomDao;
        this.roomMapper = roomMapper;
    }

    @Override
    public List<RoomVo> roomList(RoomDto roomDto) {
        return roomDao.listAll(roomDto).stream()
                .sorted(Comparator.comparing(RoomVo::getCreateTime, Comparator.nullsLast(Comparator.naturalOrder())).reversed())
                .collect(Collectors.toList());
    }

    @Override
    public RoomVo getRoomInfo(String roomId) {
        Room room = roomDao.selectById(roomId);
        return roomMapper.toVo(room);
    }

    @Override
    public String addRoomInfo(RoomAddDto roomAddDto) {
        int i = roomDao.insert(roomMapper.toAddEntity(roomAddDto));
        return String.valueOf(i);
    }

    @Override
    public String updateRoomInfo(RoomUpdateDto roomUpdateDto) {
        Room room = roomMapper.toUpdateEntity(roomUpdateDto);
        room.setRoomId(roomUpdateDto.getRoomId());
        int i = roomDao.updateById(room);
        return String.valueOf(i);
    }

    @Override
    public String deleteRoomInfo(String roomId) {
        int i = roomDao.deleteById(roomId);
        return String.valueOf(i);
    }
}
